#!/bin/bash
#set -x
#set -e

source ./config/config
source ./script/lib.sh

## redis第1个pod的角色
#kubectl exec -it wh-redis-redis-0 -c redis -n cloud-infra /bin/bash -- redis-cli role | head -n 1 | tr -d '1) ""' >> ${file_output}

## redis第2个pod的角色
#kubectl exec -it wh-redis-redis-1 -c redis -n cloud-infra /bin/bash -- redis-cli role | head -n 1 | tr -d '1) ""' >> ${file_output}

## redis第3个pod的角色
#kubectl exec -it wh-redis-redis-2 -c redis -n cloud-infra /bin/bash -- redis-cli role | head -n 1 | tr -d '1) ""' >> ${file_output}

echo -e "\033[93m|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|"
echo -e "|                        redis                     |"
echo -e "|+++++++++++++++++++++++++++++++++++++++++++++++++++++++++|\033[0m"

if [ -z ${REDIS_PASSWORD} ];then
   redis_cil="kubectl exec -it ${REDIS_NAME}-0 -c redis -n ${NAMESPACE} /bin/bash -- redis-cli"
else
   redis_cil="kubectl exec -it ${REDIS_NAME}-0 -c redis -n ${NAMESPACE} /bin/bash -- redis-cli -a ${REDIS_PASSWORD}"
fi

echo -e "#################    redis主从同步状态  #################"
#redis_sync_exception=`kubectl exec -it wh-redis-redis-0 -c redis -n cloud-infra /bin/bash -- redis-cli role | head -n 1 | tr -d '1) ""' >> ${file_output}`
redis_sync_exception=`${redis_cil} role | grep -E "master|slave" | tr -d "1)"`
echo -e "redis-0状态如下：" >>$result_dir/redis_sync_exception_detail
wDetail "$redis_sync_exception\n" redis_sync_exception

echo -e "---------------------\n" >>$result_dir/redis_sync_exception_detail

#redis_sync_exception=`kubectl exec -it wh-redis-redis-1 -c redis -n cloud-infra /bin/bash -- redis-cli role | head -n 1 | tr -d '1) ""' >> ${file_output}`
if [ -z ${REDIS_PASSWORD} ];then
   redis_sync_exception=`kubectl exec -it ${REDIS_NAME}-1 -c redis -n ${NAMESPACE} /bin/bash -- redis-cli role | grep -E "master|slave" | tr -d "1)"`
else
   redis_sync_exception=`kubectl exec -it ${REDIS_NAME}-1 -c redis -n ${NAMESPACE} /bin/bash -- redis-cli -a ${REDIS_PASSWORD} role | grep -E "master|slave" | tr -d "1)"`
fi
echo -e "redis-1状态如下：" >>$result_dir/redis_sync_exception_detail
wDetail "$redis_sync_exception\n" redis_sync_exception

echo -e "---------------------\n" >>$result_dir/redis_sync_exception_detail

#redis_sync_exception=`kubectl exec -it wh-redis-redis-2 -c redis -n cloud-infra /bin/bash -- redis-cli role | head -n 1 | tr -d '1) ""' >> ${file_output}`
if [ -z ${REDIS_PASSWORD} ];then
   redis_sync_exception=`kubectl exec -it ${REDIS_NAME}-2 -c redis -n ${NAMESPACE} /bin/bash -- redis-cli role | grep -E "master|slave" | tr -d "1)"`
else
   redis_sync_exception=`kubectl exec -it ${REDIS_NAME}-2 -c redis -n ${NAMESPACE} /bin/bash -- redis-cli -a ${REDIS_PASSWORD} role | grep -E "master|slave" | tr -d "1)"`
fi
echo -e "redis-2状态如下：" >>$result_dir/redis_sync_exception_detail
wDetail "$redis_sync_exception\n" redis_sync_exception

echo -e "#################  安全认证检查  #################"
redis_auth=`${redis_cil} CONFIG GET requirepass | grep -v requirepass`
echo -e "认证：" >>$result_dir/redis_auth_detail

redis_auth_warning=`${redis_cil} CONFIG GET requirepass | grep -v requirepass`
if [[ -n $redis_auth_warning ]];then
	wResult "NORMAL" redis_auth
	wDetail "$redis_auth\n" redis_auth
else
	wResult "WARNING" redis_auth
	wDetail "没有安全认证\n" redis_auth
fi

echo -e "---------------------\n" >>$result_dir/redis_auth_detail

redis_auth=`${redis_cil} CONFIG GET masterauth | grep -v masterauth`
echo -e "复制认证：" >>$result_dir/redis_auth_detail
if [[ -n $redis_auth ]];then
	wResult "NORMAL" redis_auth
	wDetail "$redis_auth\n" redis_auth
else
	wResult "WARNING" redis_auth
	wDetail "没有复制认证\n" redis_auth
fi

echo -e "---------------------\n" >>$result_dir/redis_auth_detail

redis_auth=`kubectl exec -it ${REDIS_NAME}-0 -c redis -n ${NAMESPACE} /bin/bash -- cat /redis/redis.conf |grep -i rename-command`
echo -e "命令重命名：" >>$result_dir/redis_auth_detail
if [[ -n $redis_auth ]];then
	wResult "NORMAL" redis_auth
	wDetail "$redis_auth\n" redis_auth
else
	wResult "WARNING" redis_auth
	wDetail "没有命令重命名\n" redis_auth
fi

echo -e "#################  持久化检查  #################"
redis_persistence=`${redis_cil} CONFIG GET save | grep -v save`
echo -e "rdb持久化：" >>$result_dir/redis_persistence_detail
wDetail "$redis_persistence\n" redis_persistence

echo -e "---------------------\n" >>$result_dir/redis_persistence_detail

redis_persistence=`${redis_cil} CONFIG GET appendonly | grep -v appendonly`
echo -e "aof持久化：" >>$result_dir/redis_persistence_detail
wDetail "$redis_persistence\n" redis_persistence

echo -e "#################  内存配置检查  #################"
echo -e "当前内存使用情况：" >>$result_dir/redis_memory_detail
redis_memory=`${redis_cil} INFO Memory | grep used_memory_human`
wDetail "$redis_memory\n" redis_memory

echo -e "---------------------\n" >>$result_dir/redis_memory_detail

echo -e "当前内存使用上限：" >>$result_dir/redis_memory_detail
redis_memory=`${redis_cil} CONFIG GET maxmemory | grep -v maxmemory | tr -d "2) \"\""`
if [[ $redis_memory == 0 ]];then
  wDetail "内存没有设置使用上限\n" redis_memory
else
  wDetail "$redis_memory\n" redis_memory
fi

echo -e "---------------------\n" >>$result_dir/redis_memory_detail

echo -e "内存淘汰策略：" >>$result_dir/redis_memory_detail
redis_memory=`${redis_cil} CONFIG GET maxmemory-policy | grep -v maxmemory-policy`
wDetail "$redis_memory\n" redis_memory

echo -e "---------------------\n" >>$result_dir/redis_memory_detail

echo -e "内存碎片率：" >>$result_dir/redis_memory_detail
redis_memory=`${redis_cil} INFO Memory | grep mem_fragmentation_ratio`
wDetail "$redis_memory\n" redis_memory

echo -e "#################  服务日志检查  #################"
redis_loglevel=`${redis_cil} CONFIG GET loglevel | grep -v loglevel`
wDetail "$redis_loglevel\n" redis_loglevel

echo -e "#################  超时时间配置  #################"
redis_timeout=`${redis_cil} CONFIG GET "timeout" | grep -v timeout`
wDetail "$redis_timeout\n" redis_timeout